package lyttest.common.redisConnect;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.Set;

public class SentinelRedisConnection implements RedisConnection {
    private final JedisSentinelPool sentinelPool;
    private final Jedis jedis;

    public SentinelRedisConnection(String masterName, Set<String> sentinels, String redisPassword) {
        this.sentinelPool = new JedisSentinelPool(masterName, sentinels, redisPassword);
        this.jedis = sentinelPool.getResource();

    }

    @Override
    public String set(String key, String value) {
        return jedis.set(key, value);
    }

    @Override
    public boolean getBit(String key, Integer offset) {
        return jedis.getbit(key, offset);
    }

    @Override
    public void close() {
        if (jedis != null) {
            jedis.close();
        }
        if (sentinelPool != null) {
            sentinelPool.close();
        }
    }
}